﻿using Sparklings.LevelElements;
using System;

namespace Sparklings.LevelEditor
{
    public struct TileListEntryData
    {
        public RayTracing.RayColor Color;
        public RayTracing.RayColor Color2;
        public int Count;
        public bool State;
    }
    public struct TileListNeededData
    {
        public bool Color;
        public bool Color2;
        public bool Count;
        public bool State;
    }
    public class TileListEntry
    {
        public delegate void OnPlacedEvent(Level l, int x, int y, TileListEntryData data);
        public TileListEntry(string name, OnPlacedEvent onPlaced, TileListNeededData neededData = new TileListNeededData())
        {
            this.name = name;
            OnPlaced = onPlaced;
            NeededData = neededData;
        }
        public override string ToString()
        {
            return name;
        }
        public void Place(Level l, int x, int y, MainForm f)
        {
            if (!l.IsInLevel(x, y)) { return; }
            RayTracing.RayColor rc = RayTracing.RayColor.None;
            if (f.comboBoxColorPicker.SelectedItem != null)
            {
                rc = (RayTracing.RayColor)Enum.Parse(typeof(RayTracing.RayColor), (string)f.comboBoxColorPicker.SelectedItem);
            }
            RayTracing.RayColor rc2 = RayTracing.RayColor.None;
            if (f.comboBoxColorPicker.SelectedItem != null)
            {
                rc2 = (RayTracing.RayColor)Enum.Parse(typeof(RayTracing.RayColor), (string)f.comboBoxColorPickerOther.SelectedItem);
            }
            OnPlaced(l, x, y,
                new TileListEntryData
                {
                    Color = rc,
                    Color2 = rc2,
                    Count = (int)f.numericUpDownSparklingsPerTile.Value,
                    State = f.radioButtonTileOn.Checked
                });
            l.Tiles[x, y].LoadGraphicsRelatedElements();
        }
        private OnPlacedEvent OnPlaced;
        private string name;
        public TileListNeededData NeededData { get; private set; }

        #region Implementations of OnPlaced
        public static void OnPlacedPlateform(Level l, int x, int y, TileListEntryData ed)
        {
            l.Tiles[x, y] = new DefaultPlatform(l, x, y);
        }
        public static void OnPlacedEmpty(Level l, int x, int y, TileListEntryData ed)
        {
            l.Tiles[x, y] = new EmptyTile(l, x, y);
        }
        public static void OnPlacedSpawner(Level l, int x, int y, TileListEntryData ed)
        {
            l.Tiles[x, y] = new Spawner(l, x, y, ed.Count, ed.Color);
        }
        public static void OnPlacedExit(Level l, int x, int y, TileListEntryData ed)
        {
            l.Tiles[x, y] = new ExitPoint(l, x, y, ed.Color);
        }
        public static void OnPlacedSpotPlateform(Level l, int x, int y, TileListEntryData ed)
        {
            var v = new SpotPlateform(l, x, y, ed.Color, ed.Color2);
            l.Tiles[x, y] = v;
        }
        public static void OnPlacedBluePlateform(Level l, int x, int y, TileListEntryData ed)
        {
            l.Tiles[x, y] = new BluePlatform(l, x, y);
        }
        public static void OnPlacedKillingTile(Level l, int x, int y, TileListEntryData ed)
        {
            l.Tiles[x, y] = new KillingTile(l, x, y);
        }
        public static void OnPlacedStarTile(Level l, int x, int y, TileListEntryData ed)
        {
            l.Tiles[x, y] = new StarTile(l, x, y);
        }
        internal static void OnPlacedFilterTile(Level l, int x, int y, TileListEntryData data)
        {
            l.Tiles[x, y] = new FilterTile(l, x, y, data.Color);
        }
        #endregion

    }
}
